package com.siss.tenancy.elastic.service;

import com.siss.tenancy.elastic.aop.AutoMapper;
import com.siss.tenancy.elastic.aop.AutoMapperEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * ${DESCRIPTION}
 * Service基类
 * @author ygw
 * @create 2017-07-11 14:49
 */
public abstract class BaseService<M extends Mapper<T>, T> {
    @Autowired
    protected M mapper;
    public void setMapper(M mapper){
        this.mapper = mapper;
    }

    public T selectOne(T entity) {
        return mapper.selectOne(entity);
    }

    public T selectById(Object id) {
        return mapper.selectByPrimaryKey(id);
    }

    @AutoMapper(value="entity", method= AutoMapperEnum.SELECT)
    public List<T> selectList(T entity) {
        return mapper.select(entity);
    }

    public List<T> selectListAll() {
        return mapper.selectAll();
    }


    public Long selectCount(T entity) {
        return new Long(mapper.selectCount(entity));
    }


    @AutoMapper(value="entity", method=AutoMapperEnum.INSERT)
    public void insert(T entity) {
        mapper.insert(entity);
    }

    @AutoMapper(value="entity", method=AutoMapperEnum.INSERT)
    @Transactional
    public void insertSelective(T entity) throws Exception{
        mapper.insertSelective(entity);
    }


	@AutoMapper(value="entity", method=AutoMapperEnum.DELETE)
    public void delete(T entity) {
        mapper.delete(entity);
    }


    public void deleteById(Object id) {
        mapper.deleteByPrimaryKey(id);
    }


	@AutoMapper(value="entity", method=AutoMapperEnum.UPDATE)
    public void updateById(T entity) {
        mapper.updateByPrimaryKey(entity);
    }

    @AutoMapper(value="entity", method=AutoMapperEnum.UPDATE)
    @Transactional
    public Integer updateSelectiveById(T entity) throws Exception{
       return mapper.updateByPrimaryKeySelective(entity);
    }

    public  List<T> selectByExample(Object example){
        return mapper.selectByExample(example);
    }
    public int selectCountByExample(Object example){
        return mapper.selectCountByExample(example);
    }

    @AutoMapper(value="entity", method=AutoMapperEnum.DELETE)
    @Transactional
    public Boolean softDeleteById(T entity){
    	return this.mapper.updateByPrimaryKeySelective(entity) == 1;
    }

    @AutoMapper(value="record", method=AutoMapperEnum.UPDATE)
    @Transactional
    public Integer updateByExampleSelective(T record, Object example){
    	return this.mapper.updateByExampleSelective(record, example);
    }

    @AutoMapper(value="record", method=AutoMapperEnum.DELETE)
    @Transactional
    public Integer deleteByExampleSelective(T record, Object example){
    	return this.mapper.updateByExampleSelective(record, example);
    }

}
